package test.h0819;

import java.util.*;

public class Main3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int[] nums = Arrays.stream(s.split(",")).mapToInt(Integer::parseInt).toArray();
        int len = Integer.parseInt(sc.nextLine());
        int m = Integer.parseInt(sc.nextLine());
        getRes(nums, len, m);
    }

    private static void getRes(int[] nums, int len, int m) {
        //输出出值的顺序
        LinkedList<Integer> linkedList = new LinkedList<>();
        for (int num : nums) {
            linkedList.addLast(num);
        }
        int i = 0;
        List<Integer> ans = new ArrayList<>();
        while (!linkedList.isEmpty()) {
            while (i < m) {
                int a = linkedList.removeFirst();
                linkedList.offerLast(a);
                i++;
            }
            if (i==m){
                Integer temp = linkedList.removeLast();
                ans.add(temp);
                //题目中的意思是将当前数值直接替换为m，而不是替换i，从i计数到7
                m=temp; //计数器置为下一个数的值
                i=0;
            }
        }
        StringJoiner joiner=new StringJoiner(",");
        for (Integer a : ans) {
            joiner.add(""+a);
        }
        System.out.println(joiner);
    }
}
